# Collect data to generate alpha-Lorenz curves, resource shares
# and multivariate Gini indices across 1989-2022
set.seed(102112516) 
# Choose size of pseudo sample
M <- 10000

# Desired resource shares matrix for fig 6
R <- t(matrix(c(0.95,0.95,0.5,0.5),nrow = 2,ncol = 2))

years <- seq(from = 1989,to = 2022,by = 3)
agg <- vector(mode="list", length = 12)
names(agg) <- as.character(years)

for (k in 1:12){
  print(c("Year: ",years[k]))
  inc_wealth <- vector(mode = "list",length = 5)
  
  for (i in 1:5){
    print(c("Impute",i))
    
    if (k < 11){
      Q <- scf89_22[[k]][[i]]
      W <- Q[,1]/sum(Q[,1])
      ot <- vquantile(Q[,c(4,5)],W)
      inc_wealth[[i]] <- ILF(Q[,c(4,5)],W,ot,M,R)
    }
    if (k > 10){
      Q <- scf89_22[[k]][[i]][,c(4,5,1)]
      X <- Q[,1:2]
      W <- Q[,3]/sum(Q[,3])
      mu <- cbind(X[,1]%*%W, X[,2]%*%W)
      X <- cbind(X[,1]/mu[1],X[,2]/mu[2])
      X <- X + cbind(runif(nrow(X), 0,0.001),runif(nrow(X), 0,0.01)) # Add very small noise for stability
      ot <- vquantile(X,W)
      inc_wealth[[i]] <- ILF(X[,1:2],W,ot,M,R)
    }
  }
  
  agg[[k]] <- inc_wealth 
}
